package com.dhcc.sds.base.controller;


import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsDataConditionEntity;
import com.dhcc.sds.base.service.ISdsDataConditionService;


/**
 * 项目数据源限定条件
 * 
 * @ClassName: SdsDataConditionController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-04-15 11:14:53
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsdatacondition")
public class SdsDataConditionController extends BaseController {

	private String PREFIX = "/backend/sds/sdsdatacondition/";

	@Autowired
	private ISdsDataConditionService sdsDataConditionService;
	

    /**
     * 跳转到项目数据源限定条件列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsdatacondition.html";
    }
	
    /**
     * 跳转到新增项目数据源限定条件
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsdatacondition_add.html";
    }
	
	/**
     * 跳转到修改项目数据源限定条件
     */
    @RequestMapping("/to_update/{sdsDataConditionId}")
    public String toUpdate(@PathVariable Long sdsDataConditionId, Model model) {
        SdsDataConditionEntity sdsDataCondition = this.sdsDataConditionService.selectById(sdsDataConditionId);
        model.addAttribute("sdsDataCondition",sdsDataCondition);
        return PREFIX + "sdsdatacondition_edit.html";
    }

    /**
     * 获取项目数据源限定条件列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsDataConditionEntity> page = new PageFactory<SdsDataConditionEntity>().defaultPage();
        List<SdsDataConditionEntity> result = this.sdsDataConditionService.page(page,params);
        page.setRecords((List<SdsDataConditionEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增项目数据源限定条件
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsDataConditionEntity sdsDataCondition) {
        if (CommonUtil.isOneEmpty(sdsDataCondition)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsDataCondition.insert();
        return R.ok();
    }

    /**
     * 删除项目数据源限定条件
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsDataConditionId) {
	
        this.sdsDataConditionService.deleteById(sdsDataConditionId);

        return R.ok();
    }

    /**
     * 修改项目数据源限定条件
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsDataConditionEntity sdsDataCondition) {
        if (CommonUtil.isOneEmpty(sdsDataCondition)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsDataConditionEntity old = this.sdsDataConditionService.selectById(sdsDataCondition.getId());

        old.setDataSourceId(sdsDataCondition.getDataSourceId());
        old.setFilterFieldId(sdsDataCondition.getFilterFieldId());
        old.setFilterParam(sdsDataCondition.getFilterParam());
        old.setFilterType(sdsDataCondition.getFilterType());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
